//定位器专项数据列表分页
import React, { Component } from 'react';
import ThePagination from "@/components/ThePagination";
import TheTable from "@/components/TheTable";
import TheButton from "@/components/TheButton";
import {
    apiGetPage, apiGetPrice, apiGetExport
} from '@/request/dataBoard/distributionNews'

import TheQueryForm from '@/components/TheQueryForm';
import TheTableExtend from '@/components/TheTableExtend'
import store from "@/store/store"
import { exportSheetTable } from '@/utils/exportTable.js';
import _ from 'lodash';
import { getDateTime3 } from '@/utils/getDate'
import { transNumber } from "@/utils/numberTrans";
import Card from "@/views/dataBoard/components/card.jsx"

class TheNumberOfStatistics extends Component {
    constructor() {
        super()
        this.formRef_search = React.createRef()
        this.queryFormRef = React.createRef()
        this.voucherFormRef = React.createRef()
        this.state = {
            price: "",
            showArr: [],
            columns: [
                {
                    title: " 业务编号",
                    dataIndex: "detailId",
                    key: "detailId",
                },
                {
                    title: "用户id",
                    dataIndex: "memberId",
                    key: "memberId",
                },
                {
                    title: "用户昵称",
                    dataIndex: "nickName",
                    key: "nickName",
                },
                {
                    title: "收支类型",
                    dataIndex: "category",
                    key: "category",
                    render: (txt, _) => {
                        let text = ''
                        switch (txt + '') {
                            case '0':
                                text = '收入'
                                break
                            case '1':
                                text = '支出'
                                break
                            default:
                                text = '/'
                                break
                        }
                        return text
                    }
                },
                {
                    title: "明细",
                    dataIndex: "detailType",
                    key: "detailType",
                    render: (txt) => {
                        let text = ''
                        switch (txt + '') {
                            case '1':
                                text = '月度奖励'
                                break
                            case '2':
                                text = '拉新活动奖励'
                                break
                            case '3':
                                text = '订单佣金'
                                break
                            case '4':
                                text = '提现退回'
                                break
                            case '5':
                                text = '提现支出'
                                break
                            case '9':
                                text = '其它支出'
                                break
                            default:
                                text = '/'
                                break
                        }
                        return text
                    }

                },
                {
                    title: "订单编号",
                    dataIndex: "orderCode",
                    key: "orderCode",
                },
                {
                    title: "新人ID",
                    dataIndex: "newMemberId",
                    key: "newMemberId",
                },
                {
                    title: "时间",
                    dataIndex: "createTime",
                    key: "createTime",
                },
                {
                    title: "金额",
                    dataIndex: "changeAmount",
                    key: "changeAmount",
                    render: (text) => {
                        return  <React.$Price price={text} />
                    }
                },
                {
                    title: "变更后金额(¥）",
                    dataIndex: "afterChangeBalance",
                    key: "afterChangeBalance",
                    render: (text) => {
                        return <React.$Price price={text} />
                    }
                },
                {
                    title: "备注",
                    dataIndex: "remark",
                    key: "remark",
                },
            ],
            dataSource: [],
            dateSource2: [],
            pageData: {
                pageNo: 1,
                pageSize: 10
            },
            total: 0,
            newColumns: [],
            form_show: true,
            searchOption: [
                { field: 'memberInfo', label: '用户id', placeholder: '', required: false, rules: [], type: 'Input', defaultValue: '' },
                { field: "category", label: "收支类型", placeholder: "全部", type: "Select", options: [{ value: '', label: '全部' }, { value: 1, label: '支出' }, { value: 0, label: '收入' }] },
                { field: 'times', label: '奖励到账时间', placeholder: '', required: false, rules: [], type: 'RangePicker', defaultValue: '' },
            ],
            queryForm: {},
        }
    }
    componentDidMount() {
        this.onColumnsFilter(this.state.columns)
        let searchParams = store.getState().searchValue
        let { pageNo, pageSize, queryForm, pathName } = searchParams
        if (searchParams && pageNo && pathName == 'distributionNews') {
            this.setState({
                queryForm,
                pageData: {
                    pageNo: pageNo, pageSize: pageSize
                }
            }, () => {
                this.queryFormRef.onSetDefault({
                    ...queryForm
                })
                this.getData()
                this.getDateS()
            })
        } else {
            this.getData()
            this.getDateS()
        }
    }
    getData(values) {
        let { pageNo, pageSize } = this.state.pageData
        if (values && !_.isEmpty(values.times)) {
            values.rewardCreditTimeStart = getDateTime3(values.times[0]) + " 00:00:00"
            values.rewardCreditTimeEnd = getDateTime3(values.times[1]) + " 23:59:59"
            delete (values.times)
        }
        this.setState({
            queryForm: values === undefined ? this.state.queryForm : { ...values }
        }, async () => {

            const params = {
                pageNo, pageSize, queryForm: this.state.queryForm
            }
            store.dispatch({
                type: 'SaveSearchValues',
                data: {
                    pathName: 'distributionNews',
                    ...params
                }
            })
            let res = await apiGetPage({
                pageNo, pageSize, ...this.state.queryForm,
            })
            if (res.code === 0) {
                this.setState({
                    dataSource: res.data.map((item, index) => {
                        return {
                            index,
                            ...item,
                        }
                    }),
                    total: res.totalCount
                })
            }
        })
    }

    pageChange = (pageNo, pageSize) => {
        this.setState({
            pageData: {
                pageNo, pageSize
            }
        }, () => {
            this.getData()
        })
    };
    onReset() {
        this.setState({
            queryForm: {}
        }, () => {
            this.pageChange(1, 10)
            this.getDateS()

        })
    }
    onColumnsFilter(value) {
        this.setState({
            newColumns: value
        })
    }
    onFormSearch(values) {
        this.setState({
            pageData: { pageNo: 1, pageSize: 10 }
        }, () => {
            this.getData(values)
            this.getDateS(values)
        })
    }
    getDateS = async (values) => {
        if (values && !_.isEmpty(values.times)) {
            values.rewardCreditTimeStart = getDateTime3(values.times[0]) + " 00:00:00"
            values.rewardCreditTimeEnd = getDateTime3(values.times[1]) + " 23:59:59"
            delete (values.times)
        }
        let res = await apiGetPrice({...values})
        if (res.code === 0) {
            let dateSource2 = [];
            dateSource2 = [
                {
                    top:
                    {
                        title: '累计奖励金额（¥）',
                        value: React.$transNum(res.data, 2, -2),
                        explain: "累计发放到账的奖励金额，保留小数点后两位",
                        yesterdayShow: true
                    },
                },
            ]
            this.setState({ dateSource2 })
        }
    }

    toExport = async () => {
        const { queryForm, total } = this.state
        const api = async () => {
            await apiGetExport({
                ...queryForm, pageNo: 1,
                pageSize: 20000
            }).then(res => {
                if (res.code === 0 && res.data) {
                    let th_arr = [
                        {
                            title: '返佣拉新专项数据',
                            prop: '',
                            children: [
                                {
                                    title: "业务编号",
                                    prop: "detailId",
                                },
                                {
                                    title: "用户id",
                                    prop: "memberId",
                                },
                                {
                                    title: "用户昵称",
                                    prop: "nickName",
                                },
                                {
                                    title: "收支类型",
                                    prop: "category",
                                },
                                {
                                    title: "明细",
                                    prop: "detailType",
                                },
                                {
                                    title: "订单编号",
                                    prop: "orderCode",
                                },
                                {
                                    title: "新人ID",
                                    prop: "newMemberId",
                                },
                                {
                                    title: "时间",
                                    prop: "createTime",
                                },
                                {
                                    title: "金额",
                                    prop: "changeAmount",
                                },
                                {
                                    title: "变更后金额(¥）",
                                    prop: "afterChangeBalance",
                                },
                                {
                                    title: "备注",
                                    prop: "remark",
                                },
                            ]
                        },
                    ]
                    let tb_arr = []
                    const getBusinessType = (txt) => {
                        let text = ''
                        switch (txt + '') {
                            case '0':
                                text = '收入'
                                break
                            case '1':
                                text = '支出'
                                break
                            default:
                                text = '/'
                                break
                        }
                        return text
                    }
                    const getDetailType = (txt) => {
                        let text = ''
                        switch (txt + '') {
                            case '1':
                                text = '月度奖励'
                                break
                            case '2':
                                text = '拉新活动奖励'
                                break
                            case '3':
                                text = '订单佣金'
                                break
                            case '4':
                                text = '提现退回'
                                break
                            case '5':
                                text = '提现支出'
                                break
                            case '9':
                                text = '其它支出'
                                break
                            default:
                                text = '/'
                                break
                        }
                        return text
                    }
                    res.data.forEach((item, index) => {
                        tb_arr.push({
                            detailId: item.detailId || '/',
                            memberId: item.memberId || '/',
                            nickName: item.nickName || '/',
                            category: getBusinessType(item.category),
                            detailType: getDetailType(item.detailType),
                            orderCode: item.orderCode || '/',
                            newMemberId: item.newMemberId || '/',
                            createTime: item.createTime || '/',
                            changeAmount: React.$transNum(item.changeAmount, 2, -2) || '/',
                            afterChangeBalance: React.$transNum(item.afterChangeBalance, 2, -2) || '/',
                            remark: item.remark || '/',
                        })
                    })
                    exportSheetTable({ fileName: '返佣拉新专项数据', tH_data: th_arr, tB_data: tb_arr, isSheet: true, merge: false })
                }
            })
        }
        React.$confirm({
            title: `最多支持导出2万条数据 当前所选择导出的数据为${total}条超出2万条将不会全部导出`,
            onOk: async () => {
                api()
            },
        });
    }
    render() {
        let { columns, dataSource, pageData, total, form_show, searchOption, newColumns, price, showArr, dateSource2 } = this.state
        return (
            <div className="p-layout-box commodity-examine">
                <TheQueryForm ref={(ref) => this.queryFormRef = ref} show={form_show} option={searchOption}
                    onReset={() => this.onReset()}
                    onGetValues={(values) => this.onFormSearch(values)} >
                </TheQueryForm>
                <div className="card-box" >
                    {
                        dateSource2.map((v, i) => {
                            return <Card key={i} top={v.top} showArr={showArr} />
                        })
                    }
                </div>
                <TheTableExtend
                    columns={columns}
                    onFormTrigger={() => this.setState({ form_show: !this.state.form_show })}
                    onTableRefresh={() => this.getData()}
                    onColumnsFilter={(value) => this.onColumnsFilter(value)}
                >
                    <TheButton haspermi={['dataBoard:distributionNews:export']} size="middle" onClick={() => this.toExport()} type="default">导出</TheButton>
                </TheTableExtend>
                <TheTable
                    scroll={{ x: "max-content", y: '63vh' }}
                    columns={newColumns}
                    dataSource={dataSource}
                    rowKey={'businessId'}
                />
                <ThePagination
                    pageNo={pageData.pageNo}
                    pageSize={pageData.pageSize}
                    total={total}
                    pageChange={this.pageChange}
                />
            </div>
        );
    }
}
export default TheNumberOfStatistics
